package cn.stylefeng.guns.modular.school.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.comm.util.DictTypeConstant;
import cn.stylefeng.guns.modular.school.entity.SchoolInfo;
import cn.stylefeng.guns.modular.school.entity.StudentGuardian;
import cn.stylefeng.guns.modular.school.model.params.StudentGuardianParam;
import cn.stylefeng.guns.modular.school.model.result.StudentGuardianResult;
import cn.stylefeng.guns.modular.school.service.StudentGuardianService;
import cn.stylefeng.guns.sys.modular.system.entity.Dict;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 学生监护人信息控制器
 *
 * @author
 * @Date 2019-11-27 15:10:17
 */
@Controller
@RequestMapping("/studentGuardian")
public class StudentGuardianController extends BaseController {

    private String PREFIX = "/school/studentGuardian";

    @Autowired
    private StudentGuardianService studentGuardianService;
    @Autowired
    private DictService dictService;

    /**
     * 跳转到主页面
     *
     * @author
     * @Date 2019-11-27
     */
    @RequestMapping("")
    public String index(int studentId) {
        setAttr("studentId", studentId);
        return PREFIX + "/studentGuardian.html";
    }

    /**
     * 新增页面
     *
     * @author
     * @Date 2019-11-27
     */
    @RequestMapping("/add")
    public String add(int studentId) {
        setAttr("studentId", studentId);
        // 监护人关系
        List<Dict> JHRGX = dictService.listDictsByCode(DictTypeConstant.JHRGX);
        setAttr("JHRGX", JHRGX);
        return PREFIX + "/studentGuardian_add.html";
    }

    /**
     * 编辑页面
     *
     * @author
     * @Date 2019-11-27
     */
    @RequestMapping("/edit")
    public String edit() {
        //setAttr("studentId", studentId);
        // 监护人关系
        List<Dict> JHRGX = dictService.listDictsByCode(DictTypeConstant.JHRGX);
        setAttr("JHRGX", JHRGX);
        return PREFIX + "/studentGuardian_edit.html";
    }

    /**
     * 新增接口
     *
     * @author
     * @Date 2019-11-27
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(StudentGuardianParam studentGuardianParam) {
        this.studentGuardianService.add(studentGuardianParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author
     * @Date 2019-11-27
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(StudentGuardianParam studentGuardianParam) {
        this.studentGuardianService.update(studentGuardianParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author
     * @Date 2019-11-27
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(StudentGuardianParam studentGuardianParam) {
        this.studentGuardianService.delete(studentGuardianParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author
     * @Date 2019-11-27
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(StudentGuardianParam studentGuardianParam) {
//        QueryWrapper<StudentGuardian> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("student_id", studentId).orderByAsc("tel");
//        List<StudentGuardian> studentGuardianList = studentGuardianService.list(queryWrapper);
//        StudentGuardianResult studentGuardianResult = new StudentGuardianResult();
//        if (studentGuardianList.size() > 1) {
//            studentGuardianResult.setName(studentGuardianList.get(0).getName());
//            studentGuardianResult.setTel(studentGuardianList.get(0).getTel());
//            studentGuardianResult.setReleation(studentGuardianList.get(0).getReleation());
//        }
//        if (studentGuardianList.size() == 2) {
//            studentGuardianResult.setNameB(studentGuardianList.get(1).getName());
//            studentGuardianResult.setTelB(studentGuardianList.get(1).getTel());
//            studentGuardianResult.setReleationB(studentGuardianList.get(1).getReleation());
//        }
        StudentGuardian detail = this.studentGuardianService.getById(studentGuardianParam.getId());
        return ResponseData.success(detail);
        //return ResponseData.success(studentGuardianResult);
    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2019-11-27
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(StudentGuardianParam studentGuardianParam) {
        return this.studentGuardianService.findPageBySpec(studentGuardianParam);
    }

}


